package mark.tom.pistols.algorithm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import mark.tom.pistols.algorithm.encryption.AESEncryption;
import mark.tom.pistols.algorithm.encryption.Base64Encryption;
import mark.tom.pistols.algorithm.encryption.DESEncryption;
import mark.tom.pistols.algorithm.encryption.RSAEncryption;

final class Tester {

    private final static String DES_KEY = "917a23940740df26";

    private final static String AES_KEY = "7714163a196869d3d6024ca134a9766f";

    private final static String RSA_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDmt9DNCDz7/zQEl7o1Ruz3e2nl70I4uqLQK/LVSgBny9jEJ0TqzWep63KDgauA3nZd6n85p8WloGQmmUZrIVIb6dCwFYsBQKTHbA5Aj3GknhVoDq5dp98nCVYI+BYFe/maJ1EGNx2jyJ1fJcCt8TthuB+U7Up4tKHQYcVGOxjMoQIDAQAB";
    private final static String RSA_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAOa30M0IPPv/NASXujVG7Pd7aeXvQji6otAr8tVKAGfL2MQnROrNZ6nrcoOBq4Dedl3qfzmnxaWgZCaZRmshUhvp0LAViwFApMdsDkCPcaSeFWgOrl2n3ycJVgj4FgV7+ZonUQY3HaPInV8lwK3xO2G4H5TtSni0odBhxUY7GMyhAgMBAAECgYBUI1zGKOb0j1ENC7ggjckFR8CK1gI0ZfvSJvTby6zyLWjTv+5mIppXYUHVq4g76uUHwui6wfBWQnPDj8oab4BddEcfHKoRubwqL73DSyx1iLB8mIXG5s6bHK4h3oMqMoZLsJspJaTezWV8SpDTJ74oMKPQGxcmnTjzsJY0TSw1AQJBAPn00pwv+9BJtz95QAdEMwVzM7J0vQk6dkbvV1rYQJbifCjwN1Oeqe0MFIqgE4dZ8AjGhJvrfqYId14hQ+FZAbkCQQDsS+luHhHduB+vY5GucN/eEGN6qaGDYKyyXRytbPoXK8mkrDJozwGLqH7VbX3qW/tm+3tbXKUXTR3JIv4ojrYpAkAtNP9GalND16FB5J8U8Vu895n5A64Q1jcAG1eUQZpk769/A/str3uSOOYaMFt31mdYXvcq8Aum4c8dAi0xu5RZAkEAwuxEHXB0sH/os1fhnxLBuX+vcEpqT+P50hD5TR6ul1qptkvOMaNB1SEjr4/o5OEH3AXr1ovwK+6TeY6CNpazMQJBAKFUny5lpRoSxIVYv5rUVFFeXNVpQeXYOUanCHmFjYUoax9ZjODnno5pzDmwFkTscgoWXPDEf6SnQX5tX1ttwPM=";

    public static void main(String[] args) {
        String input = readFileByLines("E:\\MarkProject\\Mark04\\pistols-core\\pistols-algorithm\\test\\ServerConfig.JSON");
        try {
            System.out.println("Original Data : ");
            System.out.println(input + "\n");

            System.out.println("######################DES##################################");
            System.out.println("Encrypt Data : ");
            String DESEncryptStr = DESEncryption.encrypt(DES_KEY, "12345678", input);
            System.out.println(DESEncryptStr + "\n");

            System.out.println("Decrypt Data : ");
            System.out.println(DESEncryption.decrypt(DES_KEY, "12345678", DESEncryptStr) + "\n");
            System.out.println("######################DES##################################");

            System.out.println("######################AES##################################");
            System.out.println("Encrypt Data : ");
            String AESEncryptStr = AESEncryption.encrypt(AES_KEY, input);
            System.out.println(AESEncryptStr + "\n");

            System.out.println("Decrypt Data : ");
            System.out.println(AESEncryption.decrypt(AES_KEY, AESEncryptStr)+ "\n");
            System.out.println("######################AES##################################");


            System.out.println("######################RSA##################################");
            StringBuilder sb = new StringBuilder();
            int times = input.length() / 117;
            if (input.length() % 117 != 0) {
                times++;
            }
            for (int i = 0; i < times; i++) {
                int startIndex = 117 * i;
                int endIndex = 117 * (i + 1) > input.length() ? input.length() : 117 * (i + 1);
                String subStr = input.substring(startIndex, endIndex);
                String eData = getRSAEncryptStr(subStr);
                sb.append(eData);
            }
            String encryptStr = sb.toString();
            System.out.println("Encrypt Data : ");
            System.out.println(encryptStr + "\n");

            sb = new StringBuilder();
            times = encryptStr.length() / 172;
            if (encryptStr.length() % 172 != 0) {
                times++;
            }
            for (int i = 0; i < times; i++) {
                int startIndex = 172 * i;
                int endIndex = 172 * (i + 1);
                String subStr = encryptStr.substring(startIndex, endIndex);
                sb.append(getRSADecryptStr(subStr));
            }
            System.out.println("Decrypt Data : ");
            System.out.println(sb.toString() + "\n");
            System.out.println("######################RSA##################################");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static String getRSAEncryptStr(String testData) throws Exception {
        return new String(Base64Encryption.encrypt(RSAEncryption.encrypt(RSA_PUBLIC_KEY, testData.getBytes("utf-8"))), "utf-8");
    }

    private static String getRSADecryptStr(String testData) throws Exception {
        return new String(RSAEncryption.decrypt(RSA_PRIVATE_KEY, Base64Encryption.decrypt(testData)), "utf-8");
    }

    private static String readFileByLines(String fileName) {
        StringBuilder sb = new StringBuilder();
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString;
            while ((tempString = reader.readLine()) != null) {
                sb.append(tempString);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}
